package com.java.controller.center;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.java.common.enums.YseOrNo;
import com.java.common.utils.IMOOCJSONResult;
import com.java.common.utils.PageUtils;
import com.java.common.utils.R;
import com.java.pojo.OrderItems;
import com.java.pojo.Orders;
import com.java.pojo.bo.center.OrderItemsCommentBO;
import com.java.service.center.MyCommentsService;
import com.java.service.center.MyOrdersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(value = "用户中心评价模块", tags = {"用户中心评价模块相关接口"})
@RestController
@RequestMapping("mycomments")
@AllArgsConstructor
public class MyCommentsController {
    private final MyCommentsService myCommentsService;
    private final MyOrdersService ordersService;

    @ApiOperation(value = "查询订单列表", notes = "查询订单列表", httpMethod = "POST")
    @PostMapping("/pending")
    public R pending(
            @ApiParam(name = "userId", value = "用户id", required = true)
            @RequestParam String userId,
            @ApiParam(name = "orderId", value = "订单id", required = true)
            @RequestParam String orderId) {

        // 判断用户和订单是否关联
        R checkResult = checkUserOrder(userId, orderId);
        if (Integer.parseInt(checkResult.get("status").toString()) != HttpStatus.OK.value()) {
            return checkResult;
        }

        // 判断该笔订单是否已经评价过，评价过了就不再继续
        Orders myOrder = checkResult.getData(new TypeReference<Orders>(){});
        if (myOrder.getIsComment().equals(YseOrNo.YES.code)) {
            return R.error("该笔订单已经评价");
        }
        List<OrderItems> list = myCommentsService.queryPendingComment(orderId);
        return R.ok(list);
    }


    @ApiOperation(value = "保存评论列表", notes = "保存评论列表", httpMethod = "POST")
    @PostMapping("/saveList")
    public R saveList(
            @ApiParam(name = "userId", value = "用户id", required = true)
            @RequestParam String userId,
            @ApiParam(name = "orderId", value = "订单id", required = true)
            @RequestParam String orderId,
            @RequestBody List<OrderItemsCommentBO> commentList) {
        // 判断用户和订单是否关联
        R checkResult = checkUserOrder(userId, orderId);
        if (Integer.parseInt(checkResult.get("status").toString()) != HttpStatus.OK.value()) {
            return checkResult;
        }
        // 判断评论内容list不能为空
        if (CollectionUtil.isEmpty(commentList)) {
            return R.error("评论内容不能为空！");
        }
        myCommentsService.saveComments(orderId, userId, commentList);
        return R.ok();
    }

    @ApiOperation(value = "查询我的评价", notes = "查询我的评价", httpMethod = "POST")
    @PostMapping("/query")
    public R query(@RequestParam String userId, @RequestParam(value = "page",required = false,defaultValue = "1") Integer page, @RequestParam(value = "pageSize",required = false,defaultValue = "10") Integer pageSize) {

        PageUtils pages = myCommentsService.queryMyComments(userId, page, pageSize);
        return R.ok(pages);
    }

    /**
     * 用于验证用户和订单是否有关联关系，避免非法用户调用
     */
    public R checkUserOrder(String userId, String orderId) {
        Orders order = ordersService.getOne(new LambdaQueryWrapper<Orders>().eq(Orders::getUserId,userId).eq(Orders::getId,orderId));
        if (order == null) {
            return R.error("订单不存在！");
        }
        return R.ok(order);
    }

}
